//
//  JKCellAppIndexShopsAboveBanner.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/23.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKCellAppIndexShops.h"

//
#import "ByModelIndexCarousel.h"

//
#import "JKShopsDetailVC.h"

@implementation JKCellAppIndexShops
{
    CAShapeLayer *_lineLayer;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setupUI {
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIControl *last = nil;
    for (int i = 0; i < _shops.count; i ++) {
        
        UIControl *basalView = [self basalViewWithTag:10 + i];
        [self.contentView addSubview:basalView];
        
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (last) {
                // 左侧，last是上一行的右侧那个
                if (i % 2 == 0) {
                    make.left.equalTo(@15);
                    make.right.equalTo(self.mas_centerX).offset(-10);
                    make.top.equalTo(last.mas_bottom).offset(10);
                }
                // 右侧，last是本行的左侧那个
                else {
                    make.left.equalTo(last.mas_right).offset(20);
                    make.right.equalTo(@-15);
                    make.top.equalTo(last);
                }
            } else {
                make.left.equalTo(@15);
                make.right.equalTo(self.mas_centerX).offset(-10);
                make.top.equalTo(@0);
            }
            
            // 设置cell高度
            if (i == _shops.count - 1) {
//                make.bottom.equalTo(@-10);
            }
        }];
        
        last = basalView;
    }
}

- (UIControl *)basalViewWithTag:(NSInteger)tag {
    
    UIControl *basalView = [UIControl new];
    basalView.tag = tag;
    [basalView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    
    ByModelIndexCarousel *model = _shops[tag - 10];
    
    // 商家图片
    UIImageView *iconView = [UIImageView new];
    [iconView setImageWithURL:[NSURL URLWithString:BOYE_PICTURE_VIEW_URL(model.img)] placeholderImage:DEFAULT_IMAGE completed:nil];
    iconView.layer.cornerRadius = 8;
    iconView.layer.masksToBounds = true;
    [basalView addSubview:iconView];
    
    // 名字
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = model.title;
    nameLabel.font = FONT(15);
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.textColor = rgba(77, 177, 248, 1);
    [basalView addSubview:nameLabel];
    
    // 人均消费
    UILabel *avgeLabel = [[UILabel alloc] init];
    avgeLabel.text = model.detail;
    
    avgeLabel.font = FONT(13);
    avgeLabel.textAlignment = NSTextAlignmentRight;
    avgeLabel.textColor = rgba(77, 177, 248, 1);
    [basalView addSubview:avgeLabel];
    
    // 介绍
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.text = model.notes;
    descLabel.font = FONT(13);
    descLabel.textAlignment = NSTextAlignmentLeft;
    descLabel.textColor = [UIColor lightGrayColor];
    [basalView addSubview:descLabel];
    
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(@10);
        make.height.equalTo(@((SCREEN_WIDTH - 50) / 2));
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(iconView.mas_bottom).offset(10);
        make.height.equalTo(@15);
    }];
    [avgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
        make.centerY.equalTo(nameLabel);
        make.left.equalTo(nameLabel.mas_right).offset(5);
        make.height.equalTo(@15);
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(5);
        make.left.right.equalTo(@0);
        make.height.equalTo(@15);
        
        // 设置basalView的高度
        make.bottom.equalTo(@0);
    }];
    
    
    
    return basalView;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    if (!_lineLayer) {
        _lineLayer = [CAShapeLayer layer];
        _lineLayer.lineWidth = 4;
        _lineLayer.strokeColor = TABLE_COLOR.CGColor;
        [self.layer addSublayer:_lineLayer];
    }
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.width / 2, 0)];
    [path addLineToPoint:CGPointMake(self.width / 2, self.height)];
    
    for (int i = 0; i < _shops.count; i ++) {
        if (i > 0 && i % 2 == 0) {
            UIView *subview = self.contentView.subviews[i];
            [path moveToPoint:CGPointMake(0, subview.top - 2)];
            [path addLineToPoint:CGPointMake(self.width, subview.top)];
        }
    }
    
    _lineLayer.path = path.CGPath;
}

- (void)tapAction:(UIControl *)view {
    
    ByModelIndexCarousel *model = _shops[view.tag - 10];
    
    JKShopsDetailVC *vc = [[JKShopsDetailVC alloc] init];
    vc.seller_id = model.storeid;
    [((ByBaseVC *)self.viewController) by_normalPush:vc hasNavi:YES];
}

- (void)setShops:(NSArray *)shops {
    
    if (_shops != shops) {
        _shops = shops;
        [self setupUI];
    }
}

@end
